package com.me.util;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.me.app.HomeActivity;
import com.me.app.R;
import com.me.db.MeDbManager;
import com.me.floating.FloatingButton;

public class GenNotification {

	public static void show(String packageName, Activity activity) {
		int eventCount = 0;		
		// Custom layout of the notification
		RemoteViews remoteViews = new RemoteViews(packageName,
				R.layout.notification);
		
		eventCount = new MeDbManager(activity.getBaseContext()).countEvents();
			
		if (eventCount == 1)
			remoteViews.setTextViewText(R.id.Noti_status, "You've got 1 pending event.");
		else if (eventCount >= 2)
			remoteViews.setTextViewText(R.id.Noti_status, "You've got " + eventCount +" pending events.");
		else
			remoteViews.setTextViewText(R.id.Noti_status, "You've got no pending event.");
		
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				activity).setSmallIcon(R.drawable.ic_menobackground)
				.setContent(remoteViews)
				.setVisibility(NotificationCompat.VISIBILITY_SECRET);
	
		// Open the main App when the notification is clicked
		Intent resultIntent = new Intent(activity, HomeActivity.class);
		resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TASK);

		PendingIntent resultPendingIntent = PendingIntent.getActivity(activity,
				0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		mBuilder.setContentIntent(resultPendingIntent);

		// Turn on the floating button when ON button is clicked
		Intent buttonOnIntent = new Intent(activity, FloatingButton.class);
		buttonOnIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TASK);

		PendingIntent openPendingIntent = PendingIntent.getService(activity, 0,
				buttonOnIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		remoteViews.setOnClickPendingIntent(R.id.onbutton, openPendingIntent);

		// Show the notification
		NotificationManager mNotifyMgr = (NotificationManager) activity
				.getSystemService("notification");
		mNotifyMgr.notify(1, mBuilder.build());		
	}
}
